package com.gayworld.service.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gayworld.controller.vo.RateVO;
import com.gayworld.model.dao.PlacesDAO;
import com.gayworld.model.dao.RateDAO;
import com.gayworld.model.entity.Places;
import com.gayworld.model.entity.Rate;

@Service
@Transactional
public class RateServiceImpl implements RateService{
	

	@Autowired
	private RateDAO rateDAO;
	
	@Autowired
	private PlacesDAO placesDAO;
	
	@Override
	public boolean addComment(Integer idPlace, String comment, Float rate,String ipAddress){
		Rate r = new Rate();
		r.setComment(comment);
		r.setRate(rate);
		Places p = placesDAO.findById(Places.class, idPlace);

		r.setPlace(p);
		r.setRegisterDate(new Date());
		r.setUserIP(ipAddress);
		
		return rateDAO.save(r) != null ? true : false;	
	}

	@Override
	public List<RateVO> getRatesByPlaceId(Integer placeId) {		
		List<RateVO> rateList = new ArrayList<RateVO>();		
		
		for(Rate source : rateDAO.getRatesByPlaceId(placeId)){
			RateVO target = new RateVO();
			BeanUtils.copyProperties(source, target);
			rateList.add(target);
		}
		
		return rateList;
	}
}
